// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import org.w3c.dom.Element;
import zombie.debug.DebugLog;
import zombie.debug.DebugType;
import zombie.util.PZXmlUtil;
import zombie.util.StringUtils;
import zombie.util.list.PZArrayUtil;

public final class ActionState {
    public final String name;
    public final ArrayList<ActionTransition> transitions = new ArrayList();
    private String[] m_tags;
    private String[] m_childTags;
    private static final Comparator<ActionTransition> transitionComparator = (actionTransition0, actionTransition1) -> actionTransition1.conditions.size()
            - actionTransition0.conditions.size();

    public ActionState(String _name) {
        this.name = _name;
    }

    public final boolean canHaveSubStates() {
        return !PZArrayUtil.isNullOrEmpty(this.m_childTags);
    }

    public final boolean canBeSubstate() {
        return !PZArrayUtil.isNullOrEmpty(this.m_tags);
    }

    public final boolean canHaveSubState(ActionState child) {
        return canHaveSubState(this, child);
    }

    public static boolean canHaveSubState(ActionState parent, ActionState child) {
        String[] string0 = parent.m_childTags;
        String[] string1 = child.m_tags;
        return tagsOverlap(string0, string1);
    }

    public static boolean tagsOverlap(String[] string0, String[] string1) {
        if (PZArrayUtil.isNullOrEmpty(string0)) {
            return false;
        } else if (PZArrayUtil.isNullOrEmpty(string1)) {
            return false;
        } else {
            boolean _boolean = false;

            for (int int0 = 0; int0 < string0.length; int0++) {
                String string2 = string0[int0];

                for (int int1 = 0; int1 < string1.length; int1++) {
                    String string3 = string1[int1];
                    if (StringUtils.equalsIgnoreCase(string2, string3)) {
                        _boolean = true;
                        break;
                    }
                }
            }

            return _boolean;
        }
    }

    public String getName() {
        return this.name;
    }

    public void load(String stateFolderPath) {
        File file0 = new File(stateFolderPath).getAbsoluteFile();
        File[] file1 = file0.listFiles((var0, string) -> string.toLowerCase().endsWith(".xml"));
        if (file1 != null) {
            for (File file2 : file1) {
                this.parse(file2);
            }

            this.sortTransitions();
        }
    }

    public void parse(File file) {
        ArrayList arrayList0 = new ArrayList();
        ArrayList arrayList1 = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        String string = file.getPath();

        try {
            Element element = PZXmlUtil.parseXml(string);
            if (ActionTransition.parse(element, string, arrayList0)) {
                this.transitions.addAll(arrayList0);
                if (DebugLog.isEnabled(DebugType.ActionSystem)) {
                    DebugLog.ActionSystem.debugln("Loaded transitions from file: %s", string);
                }

                return;
            }

            if (this.parseTags(element, arrayList1, arrayList2)) {
                this.m_tags = PZArrayUtil.concat(this.m_tags, (String[])arrayList1.toArray(new String[0]));
                this.m_childTags = PZArrayUtil.concat(this.m_childTags, (String[])arrayList2.toArray(new String[0]));
                if (DebugLog.isEnabled(DebugType.ActionSystem)) {
                    DebugLog.ActionSystem.debugln("Loaded tags from file: %s", string);
                }

                return;
            }

            if (DebugLog.isEnabled(DebugType.ActionSystem)) {
                DebugLog.ActionSystem.warn("Unrecognized xml file. It does not appear to be a transition nor a tag(s). %s", string);
            }
        } catch (Exception exception) {
            DebugLog.ActionSystem.error("Error loading: " + string);
            DebugLog.ActionSystem.error(exception);
        }
    }

    private boolean parseTags(Element element, ArrayList<String> arrayList0, ArrayList<String> arrayList1) {
        arrayList0.clear();
        arrayList1.clear();
        if (element.getNodeName().equals("tags")) {
            PZXmlUtil.forEachElement(element, elementx -> {
                if (elementx.getNodeName().equals("tag")) {
                    arrayList1.add(elementx.getTextContent());
                }
            });
            return true;
        } else if (element.getNodeName().equals("childTags")) {
            PZXmlUtil.forEachElement(element, elementx -> {
                if (elementx.getNodeName().equals("tag")) {
                    arrayList1.add(elementx.getTextContent());
                }
            });
            return true;
        } else {
            return false;
        }
    }

    public void sortTransitions() {
        this.transitions.sort(transitionComparator);
    }

    public void resetForReload() {
        this.transitions.clear();
        this.m_tags = null;
        this.m_childTags = null;
    }
}
